package com.rathgael.hoteldata.domain;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.rathgael.hoteldata.Settings;

public class County {

    private static Map<String, County> countymap = new HashMap<String, County>();

    public static County getByCountyCode(String countyCode) {
        return countymap.get(countyCode);
    }

    static {
        try {
            FileReader reader = new FileReader(new File(Settings.COUNTY_SOURCE));
            BufferedReader bufferedReader = new BufferedReader(reader);

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String entry = line.trim();
                County county = new County();

                county.setCountyCode(entry.substring(0, 5));
                county.setCountyName(entry.substring(7));

                // Format County Name
                county.setCountyName(county.getCountyName().substring(0, 1) + county.getCountyName().toLowerCase().substring(1));
                countymap.put(county.getCountyCode(), county);
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String countyCode;
    private String countyName;

    public String getCountyCode() {
        return countyCode;
    }

    public void setCountyCode(String countyCode) {
        this.countyCode = countyCode;
    }

    public String getCountyName() {
        return countyName;
    }

    public void setCountyName(String countyName) {
        this.countyName = countyName;
    }

}